package ex.mlist;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RlistActivity extends Activity {
	
	ListView musiclist;
	Cursor musiccursor;
	int music_column_index;
	int count;
	
	String filename;
	Button set;
	
	int strt_hrs;
	int strt_min;
	int end_hrs;
	int end_min;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rlist);
		
		
		strt_hrs=getIntent().getExtras().getInt("strt_hrs");
		strt_min=getIntent().getExtras().getInt("strt_min");
		end_hrs=getIntent().getExtras().getInt("end_hrs");
		end_min=getIntent().getExtras().getInt("end_min");
		init_phone_tones();
	}
	private void init_phone_tones() {
		System.gc();
		String[] proj = { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Video.Media.SIZE };
		musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				proj, null, null,null);
		count = musiccursor.getCount();
	
		musiclist = (ListView) findViewById(R.id.PhoneMusicList);
		musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

		musiclist.setOnItemClickListener(musiclistener);
	}
	
	private OnItemClickListener musiclistener = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position,
				long id) {
			System.gc();
			TextView tv=(TextView) findViewById(R.id.textView1);
			music_column_index = musiccursor
					.getColumnIndex(MediaStore.Audio.Media.DATA);
			musiccursor.moveToPosition(position);
			filename = musiccursor.getString(music_column_index);

			music_column_index = musiccursor
					.getColumnIndex(MediaStore.Audio.Media.SIZE);
			String name = musiccursor.getString(musiccursor
					.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
			
			//Toast.makeText(this,name,5).show();
			Intent intent=new Intent(RlistActivity.this,SetTime.class);
			intent.putExtra("strt_hrs",strt_hrs);
			
			intent.putExtra("strt_min", strt_min);
			intent.putExtra("end_hrs", end_hrs);
			intent.putExtra("end_min", end_min);
			intent.putExtra("ringtone_src", name);
			intent.putExtra("size", music_column_index);
		
			startActivity(intent);
		}
		
	};
	
	
	public class MusicAdapter extends BaseAdapter {
		private Context mContext;

		public MusicAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return count;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			System.gc();
			TextView tv = new TextView(mContext.getApplicationContext());
			String id = null;
			if (convertView == null) {
				musiccursor.moveToFirst();
				music_column_index = musiccursor
						.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
				musiccursor.moveToPosition(position);
				id = musiccursor.getString(music_column_index)+"\n";
				tv.setText(id);
			} else
				tv = (TextView) convertView;
			tv.setText(id);
			return tv;
		}
	}
}
